package cn.pconline.autoclub.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.pconline.autoclub.entity.Activity;
import cn.pconline.autoclub.entity.ActivityPlace;
import cn.pconline.autoclub.service.ActivityPlaceService;
import cn.pconline.autoclub.service.ActivityService;
import cn.pconline.autoclub.util.AppUtils;
import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.WebUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2015年2月6日 下午5:16:54
 */
@Controller
@RequestMapping(value = "/activity")
public class ActivityPlaceController {
	
	@Autowired
	private ActivityService activityService;
	@Autowired
	private ActivityPlaceService activityPlaceService;

	private static Logger log = LoggerFactory.getLogger(ActivityPlaceController.class);
	
	private void printMsg(HttpServletResponse resp, String str, String callback) {
        callback = EncodeUtils.encodeForHTMLFilterNull(callback);
        try {
            java.io.PrintWriter w = resp.getWriter();
            if (StringUtils.isNotBlank(callback)) {
                w.print(callback + "(" + str + ")");
            } else {
                w.print(str);
            }
            w.flush();
            w.close();
        } catch (IOException ex) {
            log.error("response输出出错了", ex);
        }
    }

    private void printSuccessMsg(HttpServletResponse resp, String msg, JSONObject data, String callback) {
        JSONObject json = new JSONObject();
        json.put("code", AppUtils.STATUS_OK);
        if (StringUtils.isNotEmpty(msg)) {
            json.put("message", msg);
        }
        if (data != null) {
            json.put("data", data);
        }
        printMsg(resp, json.toJSONString(), callback);
    }

    private void printErrorMsg(HttpServletResponse resp, int code, String msg, String callback) {
        JSONObject json = new JSONObject();
        json.put("code", code);
        json.put("message", msg);
        printMsg(resp, json.toJSONString(), callback);
    }

    private void printErrorMsg(HttpServletResponse resp, String msg, String callback) {
        printErrorMsg(resp, AppUtils.STATUS_FAIL, msg, callback);
    }
	
	@ResponseBody
    @RequestMapping(value = "/listDestinations.do", method = RequestMethod.GET)
    public void listDestinations(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding(request, response);
        long activityId = WebUtils.paramLong(request, "activityId", 0L);
        
        JSONObject jsonObject = new JSONObject();
        
        //活动判断
        Activity activity = activityService.findById(activityId);
        if(activity == null) {
        	printErrorMsg(response, "活动不存在", "");
        }
        //集合地点
        List<ActivityPlace> gathers = activityPlaceService.listPlaceByActivityAndType(activity,ActivityPlace.TYPE_GATHERING);
    	//目的地点
        List<ActivityPlace> destinations = activityPlaceService.listPlaceByActivityAndType(activity,ActivityPlace.TYPE_DESTINATION);
    	jsonObject.put("gathers", gathers);
    	jsonObject.put("destinations", destinations);
    	
        printSuccessMsg(response, "", jsonObject, null);
    }
	
}
